//
//  PhotoBrowserAnimator.swift
//  01- 搭建框架
//
//  Created by apple on 15/10/28.
//  Copyright © 2015年 任伟. All rights reserved.
//

import UIKit

/// MARK: - 展现动画协议
protocol PhotoBrowserPresentDelegate: NSObjectProtocol {

    /// 指定 indexPath 对应的 imageView ,用来做动画效果
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView
    
    /// 动画转场的起始位置
    func photoBrowserPresentFromRect(indexPath: NSIndexPath) -> CGRect
    
    /// 动画转场的目标位置
    func photoBrowserPresentToRect(indexPath: NSIndexPath) -> CGRect
}

/// MARK: - 解除动画协议
protocol PhotoBrowserDismissDelegate: NSObjectProtocol {
    
    // 解除转场的图像视图
    func imageViewForDismiss() -> UIImageView
    // 解除转场的图像索引
    func indexPathForDismiss() -> NSIndexPath

}

/// 提供动画相关的方法, 提供动画转场的`代理`
class PhotoBrowserAnimator: NSObject, UIViewControllerTransitioningDelegate {

    // 展现代理
    weak var presentDelegate: PhotoBrowserPresentDelegate?
    
    // 解除代理
    weak var dismissDelegate: PhotoBrowserDismissDelegate?
    // 动画图像的索引
    var indexPath: NSIndexPath?
    
    // 判断是否标记
    private var isPresented = false
    
    /// MARK: - 设置代理想伏案方法
    func setDelegateParame(presentDelegate: PhotoBrowserPresentDelegate, indexPath: NSIndexPath,
        dismissDelegate: PhotoBrowserDismissDelegate) {
            
            self.presentDelegate = presentDelegate
            self.dismissDelegate = dismissDelegate
            self.indexPath = indexPath
    }
    
    // 返回提供 modal 展现的"动画对象"
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        return self
    }
    
    // 返回 提供解除转场动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        
        return self
    }
}

/// MARK: -
extension PhotoBrowserAnimator: UIViewControllerAnimatedTransitioning {

    // 动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 0.5
    }
    
    // 实现具体的动画效果
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
//        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
//        print(fromVC)
//
//        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
//        print(toVC)
        
        isPresented ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
        
    }
    
    /// 展现转场动画
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let presentDelegate = presentDelegate, indexPath = indexPath else {
            return
        }
        
        /// 1. 目标视图
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        transitionContext.containerView()?.addSubview(toView)
        
        /// 2. 图像视图
        let iv = presentDelegate.imageViewForPresent(indexPath)
        
        // 定制图像视图的位置
        iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
        
        // 将图像视图添加到容器视图里边
        transitionContext.containerView()?.addSubview(iv)
        
        toView.alpha = 0.0
        
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            
            iv.frame = presentDelegate.photoBrowserPresentToRect(indexPath)
            
            toView.alpha = 1.0
            
            }) { _ in
                
                // 将图像视图删除
                iv.removeFromSuperview()
                
                transitionContext.completeTransition(true)
        
        }
    }
    
    /// 解除转场动画
    ///
    /// - parameter transitionContext: 转场上下文
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let presentDelegate = presentDelegate, dismissDelegate = dismissDelegate else {
                return
        }
        
        // 1. 获取 dismiss 的控制器的视图
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        fromView.removeFromSuperview()
        
        // 2. 获取图像视图
        let iv = dismissDelegate.imageViewForDismiss()
        transitionContext.containerView()?.addSubview(iv)
        
        // 3. 获取 dismiss 的 indexPath
        let indexPath = dismissDelegate.indexPathForDismiss()
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {() -> Void in
            
            // 让 iv 运动到目标视图
            iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
            
            }) { (_) -> Void in
                
                iv.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
}